import { HttpContext } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ALLOW_ANONYMOUS } from '@delon/auth';
import { SFSchema, SFUISchema, SFSelectWidgetSchema, SFRadioWidgetSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Api } from 'src/app/shared/api/api';
import {
  USER_LABEL_ENUM,
  DELETED_STATUS_ENUM,
  VCATEGORIES,
  XS_SOURCE,
  SIX,
  DEPARTMENT_LEVEL_STATUS_BADGE,
  LEVEL_STATUS_BADGE,
  ROLE_TYPE
} from 'src/app/shared/constant/common-constant';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-system-book-edit',
  templateUrl: 'user-book-edit.component.html'
})
export class BookEditComponent implements OnInit {
  record: any = {};
  readonly: boolean = false;
  userLabel: any = [];
  userStatus: any = [];
  xs_source_list: any = [];
  six_list: any = [];
  level_list: any = [];
  role_list: any = [];
  schema: SFSchema = { properties: {} };
  ui: SFUISchema = {
    '*': {
      spanLabel: 10,
      spanControl: 14,
      grid: { span: 12 }
    }
  };

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient) {}

  ngOnInit(): void {
    this.readonly = this.record.id && this.record.id.length > 0;
    this.userLabel = USER_LABEL_ENUM.filter(item => item.value.length > 0);
    this.userStatus = VCATEGORIES.filter(item => item.value !== null);
    this.xs_source_list = XS_SOURCE.filter(item => item.value !== null);
    this.six_list = SIX.filter(item => item.value !== null);
    this.level_list = LEVEL_STATUS_BADGE.filter(item => item.value !== null);
    this.role_list = ROLE_TYPE.filter(item => item.value !== null);
    this.initSchema();
  }

  // 初始化表单，在获取完部门树数据之后再初始化
  initSchema() {
    this.schema = {
      properties: {
        sys_role_id: {
          type: 'string',
          title: '角色',
          enum: this.role_list,
          default: 2
        },
        department_level_id: {
          type: 'string',
          title: '部门等级',
          enum: this.level_list,
          default: false
        },
        user_name: {
          type: 'string',
          title: '用户名',
          readOnly: this.readonly
        },
        age: {
          type: 'string',
          title: '年龄'
        },
        sex: {
          type: 'string',
          title: '性别',
          enum: this.six_list,
          default: '男',
          ui: {
            widget: 'select'
          } as SFSelectWidgetSchema
        },
        phone: {
          type: 'string',
          title: '手机号',
          pattern: '^1\\d{10}$',
          readOnly: this.readonly
        },
        address: {
          type: 'string',
          title: '地址',
          readOnly: this.readonly
        },
        account: {
          type: 'string',
          title: '账号',
          readOnly: this.readonly
        },
        password: {
          type: 'string',
          title: '密码',
          readOnly: this.readonly
        }
      },
      required: ['account', 'password', 'sys_role_id', 'department_level_id']
    };
  }

  save(value: any) {
    console.log(value);
    if (this.record.id) {
      this.http.post(Api.BaseAddUserApi, value, {}, { context: new HttpContext().set(ALLOW_ANONYMOUS, true) }).subscribe((res: any) => {
        if (res) {
          if (res.code === ResponseCode.SUCCESS) {
            this.msgSrv.success('修改成功');
            this.modal.close(true);
          } else {
            this.msgSrv.warning(res.message);
          }
        } else {
          this.msgSrv.error('修改失败，未知错误');
        }
      });
    } else {
      this.add(value);
    }
  }
  add(value: any) {
    this.http
      .post(
        Api.BaseAddUserApi,
        value,
        {},
        {
          headers: {
            Authorization: 'Bearer dGVzdDp0ZXN0'
          },
          context: new HttpContext().set(ALLOW_ANONYMOUS, true)
        }
      )
      .subscribe((res: any) => {
        if (res && res.code === ResponseCode.SUCCESS) {
          this.msgSrv.success('保存成功');
          this.modal.close(true);
        }
      });
  }
  close() {
    this.modal.destroy();
  }
}
